/////////////////////////////////////////////////////////////////////////////////////////////
* This do file generates Table A.1 and Table 2
/////////////////////////////////////////////////////////////////////////////////////////////

use "${inputdir}/prospects_analysis_dataset_clean.dta", clear

hist sfc_totalAttendance, legend(off) graphregion(color(white)) fraction ytitle("Proportion") /// 
	ylabel(, format(%02.1f) angle(0)) xlabel(0(1)16) xtitle("SFC Group Meetings Attended") fcolor(gs0) ///
	lcolor(gs0) discrete width(0.5)
	
graph export "${outputdir}/figures/Figure_1.pdf", replace

/////////////////////////////////////////////////////////////////////////////////////////////		
*								 TABLE A.1								
/////////////////////////////////////////////////////////////////////////////////////////////

* Basic Demographics
local balancevars1 = "female reg_age r1_hoh r1_hhmbr_total r1_livemother r1_livefather r1_havechildren" // demographic
local balancevars2 = "christian muslim r1_insamplefriendtot " // network

* Education and Cognitive Ability
local balancevars3 = "r1_primarycomplete r1_secondarycomplete r1_HighGrade  " // Education
local balancevars4 = "r1_numeracytotal r1_digits_ftotal r1_digits_btotal " // cognition r1_raven_total 
local balancevars5 = "r1_word1_total r1_word2_total r1_raven_total r1_numeracytotal r1_riskaversion" // memory

* Psycho-Social Measures
local balancevars6 = "r1_welfare_today r1_selfesteem r1_locuscontrol r1_aggression r1_riskybehavior r1_DASS r1_psi_std" // psychosocial

* Labor Force Participation and Finances
local balancevars7 = "lfi1 working1 hrsworked_trim1 hrsworkedpos hrsworked_ihst1 earnedincpos inc7d1_trim1 inc7d_ihst1 inc90d1_trim1  earnedincpos2 inc90d_ihst1  "

* Attrition variables
local attritvars1 = "female reg_age r1_hoh r1_hhmbr_total r1_livemother r1_livefather r1_havechildren christian muslim r1_insamplefriendtot r1_HighGrade r1_numeracytotal"
local attritvars2 = "r1_welfare_today r1_selfesteem r1_locuscontrol r1_aggression r1_riskybehavior r1_tothourswork_7d inc7d_raw1"
local attritvars3 = "lfi1 r1_psi_std"

preserve
	
	replace r1_hoh = 1 if r1_hoh!=0
	replace r1_livemother = 0 if r1_livemother!=1
	replace r1_livefather = 0 if r1_livefather!=1
	replace r1_HighGrade = 0 if r1_HighGrade == .d
	replace r1_tothourswork_7d = 0 if inlist(r1_tothourswork_7d, .a, .b, .c)

	foreach v in `attritvars1' {
		gen het1x`v' = ANYSFC * `v'
		}
	
	foreach v in `attritvars2' {
		gen het2x`v' = ANYSFC * `v'
		}
		
	foreach v in `attritvars3' {
		gen het3x`v' = ANYSFC * `v'
		}		
		
	gen attrit = r2_start_time==""	

	reg attrit ANYSFC
	estadd loc c = "No"
	estadd loc d = "No"	
	estadd scalar p = -99	
	estadd ysumm
	eststo r1
	
	reg attrit ANYSFC `attritvars1' `attritvars3'
	estadd loc c = "Yes"
	estadd loc d = "Indices"	
	estadd scalar p = -99	
	estadd ysumm
	eststo r2
	
	reg attrit ANYSFC `attritvars1' `attritvars3' het1x* het3x*
	estadd loc c = "Yes"	
	estadd loc d = "Indices"		
	estadd ysumm
	test ANYSFC + het1xfemale +het1xreg_age +het1xr1_hoh +het1xr1_hhmbr_total +het1xr1_livemother +het1xr1_livefather +het1xr1_havechildren+ het1xchristian +het1xmuslim +het1xr1_insamplefriendtot + het1xr1_HighGrade+ het1xr1_numeracytotal + het3xlfi1 + het3xr1_psi_std= 0	
	loc k = r(p)
	estadd scalar p = `k'
	eststo r3		
	
	# delimit ;
		esttab r1 r2 r3 using "${outputdir}/tables/Table_A1.tex", replace b(%10.3f) se(%10.3f) scalars(
		"ymean Mean Y" "p F-Test" "N Individuals" "c Controls"  "r2 R2")	sfmt(%10.2f %10.4f %10.0f %10.0f %10.3f) star(* 0.10 ** 0.05 *** 0.01)
		order(ANYSFC het3* het1*)  varlabel(ANYSFC "SFC" het1xfemale "Female x SFC" het1xreg_age "Age x SFC" het1xr1_hoh "Head of Household x SFC" het1xr1_hhmbr_total "Household Size x SFC" het1xr1_livemother "Mother Living x SFC" het1xr1_livefather "Father Living x SFC" het1xr1_havechildren "Has Children x SFC" het1xchristian "Christian x SFC" het1xmuslim "Muslim x SFC" het1xr1_insamplefriendtot "Friends in Sample x SFC" het1xr1_HighGrade "Education Attainment x SFC" het1xr1_numeracytotal "Numeracy x SFC" het3xlfi1 "Baseline LFI x SFC" het3xr1_psi_std "Baseline PSI x SFC" ) 
		mlabels() keep(ANYSFC het3* het1*)
		align(cccccccccccc) title(Test for Selective Attrition);
	# delimit cr		

restore
	
/////////////////////////////////////////////////////////////////////////////////////////////		
*								 TABLE 2								
/////////////////////////////////////////////////////////////////////////////////////////////	
	
	
preserve 
	keep `balancevars1' `balancevars2' `balancevars3' `balancevars4' CFW BOTH ANYSFC SFC ///
		`balancevars5' `balancevars6' `balancevars7' r2_consent  

	order `balancevars1' `balancevars2' `balancevars3' `balancevars4' CFW BOTH ANYSFC SFC ///
		`balancevars5' `balancevars6' `balancevars7'		
			
	sum r1_welfare_today, detail
	gen welftoday_index = (r1_welfare_today - r(mean)) / r(sd)
	replace r1_welfare_today = welftoday_index
	drop welftoday_index *consent*
	
	estpost ttest * , by(ANYSFC)
	estimates store sumtable
	label var hrsworkedpos "Hours worked last week (if positive)"
	label var earnedincpos "Income earned last week (if positive)"
	label var earnedincpos2 "Income earned last month (if positive)"
	label var inc90d1_trim1 "Income last three months trim top 1\%"
	label var inc7d1_trim1 "Income last seven days trim top 1\%"	
	label var hrsworked_trim1 "Income last three months trim top 1\%"
	
	count if ANYSFC==0
	local ctr = `r(N)'
	count if ANYSFC==1
	local trt = `r(N)'

	esttab sumtable using "${outputdir}/tables/Table_2.tex", cells("mu_1(fmt(3) label(Control)) mu_2(fmt(3) label(Treatment)) p(fmt(2) label(P-value of Diff) star)") ///
		varlabels() ///
		order() drop(ANYSFC CFW SFC BOTH ) ///
		replace noobs star(* 0.10 ** 0.05 *** 0.01) scalars(N) label nonumbers alignment(ccccc) ///
		keep() note(Control=`ctr' Treat=`trt') title(Baseline Balance by Treatment Status)
restore
